package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.*;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.AdminCenterMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminCenterService;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AdminCenterServiceImpl implements AdminCenterService {
    @Autowired
    private AdminCenterMapper adminCenterMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private WebSocketServer webSocketServer;
    /**
     * 分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    public PageResult pageQuery(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
        Page<Orders> page = adminCenterMapper.pageQuery(ordersPageQueryDTO);
        List<OrderVO> orderVO1 = new ArrayList<>();
        for (Orders orders : page) {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(orders,orderVO);
            List<OrderDetail> list = adminCenterMapper.orderDetail(orders.getId());
            orderVO.setOrderDetailList(list);
            orderVO1.add(orderVO);
        }
        return new PageResult(page.getTotal(),orderVO1);
    }

    /**
     * 查看订单详情
     * @param id
     * @return
     */
    @Override
    public OrderVO details(Long id) {
        OrderVO orderVO = adminCenterMapper.details(id);
        List<OrderDetail> list = adminCenterMapper.orderDetail(id);
        orderVO.setOrderDetailList(list);
        return orderVO;
    }

    /**
     * 再来一单
     * @param id
     */
   /* @Override
    public void repetition(Long id) {
        List<ShoppingCartDTO> shoppingCartDTOList = adminCenterMapper.getShoppingCartById(id);
        for (ShoppingCartDTO shoppingCartDTO : shoppingCartDTOList) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            List<ShoppingCart> list = shoppingCartMapper.showShoppingCart(shoppingCart);
            *//**
             * 已存在商品
             *//*
            if (list != null && list.size() > 0){
                shoppingCart = list.get(0);
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                shoppingCartMapper.updateNumberById(shoppingCart);

            }else {
                *//**
                 * 不存在商品，插入一条数据
                 *//*
                //判断添加的是菜品还是套餐
                Long dishId = shoppingCartDTO.getDishId();
                if (dishId != null) {
                    //添加菜品
                    Dish dish = dishMapper.getById(dishId);
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setAmount(dish.getPrice());
                    shoppingCart.setImage(dish.getImage());

                } else {
                    //添加套餐
                    Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                    shoppingCart.setName(setmeal.getName());
                    shoppingCart.setAmount(setmeal.getPrice());
                    shoppingCart.setImage(setmeal.getImage());
                }
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);
                shoppingCartMapper.insert(shoppingCart);

            }
        }

        }*/
    public void repetition(Long id){
        //查询当前用户id
        Long userId = BaseContext.getCurrentId();

        //根据订单id查询当前订单详情
        List<OrderDetail> orderDetailList = adminCenterMapper.orderDetail(id);
        List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(x ->{
            ShoppingCart shoppingCart = new ShoppingCart();
            // 将原订单详情里面的菜品信息重新复制到购物车对象中
            BeanUtils.copyProperties(x, shoppingCart, "id");
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartMapper.insertBatch(shoppingCartList);
    }

    /**
     * 客户催单
     * @param id
     */
    public void reminder(Long id) {
        Orders orders = adminCenterMapper.details(id);
        if(orders == null){
            throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
        }

        Map map = new HashMap();
        map.put("type",2);
        map.put("orderId",orders.getId());
        map.put("content","订单号：" + orders.getNumber());

        String json = JSON.toJSONString(map);
        //向客户端推送消息
        webSocketServer.sendToAllClient(json);
    }
}
